let Excel = require('exceljs')

let ExcelUtil = function () {}

ExcelUtil.prototype.getData = (filePath) => {
  return new Promise((resolve, reject) => {
    let workbook = new Excel.Workbook()
    workbook.xlsx.readFile(filePath).then(() => {
      let worksheet = workbook.worksheets[0];
      let dataList = []
      worksheet.eachRow((row, rowNumber) => {
        if (rowNumber > 1) {
          let rowData = []
          row.eachCell({
            includeEmpty: true
          }, (cell, colNumber) => {
            let value = cell.value;
            if (!value) {
                value = '';
            }
            rowData.push(value);
          });
          dataList.push(rowData)
        }        
      })
      resolve(dataList);
    }, (err) => {
      reject(new Error("文件读取失败，请检查文件格式是否正确。"))
    })
  })
}

ExcelUtil.prototype.createWorkBook = () => {
  let workbook = new Excel.Workbook()
  for (let i = 1, len = 4; i < len; i++) {
    workbook.addWorksheet('Sheet' + i)
  }
  return workbook
}

function calcRowHeight (rowData) {
  var count = 0;
  for (let index = 0; index < rowData.length; index++) {
    if (rowData[index]) {
      let data = rowData[index]
      let rows = String(data).split("\n").length - 1
      if (rows > count) {
        count = rows
      }
    }
  }
  if (count === 0) {
    return 15
  } else {
    return 15 * (count + 1)
  }
}

ExcelUtil.prototype.setDataRowStyle = function (worksheet, rowData, startRowIndex = 2) {
  for (var i = 0; i < rowData.length; i++) {
    var row = worksheet.getRow(i + startRowIndex);
    row.height = calcRowHeight(rowData[i]);
    row.font = {
      name: 'Microsoft YaHei UI',
      color: {
        argb: 'FF000000'
      },
      size: 9
    };
    row.alignment = {
      vertical: 'middle'
    };
  }
  worksheet.eachRow(function(row, rowNumber) {
    row.eachCell({
      includeEmpty: true
    }, function (cell) {
      if(rowNumber >= startRowIndex) {
        cell.border = {
          top: {style:'thin'},
          left: {style:'thin'},
          bottom: {style:'thin'},
          right: {style:'thin'}
        };
        cell.alignment = { wrapText: true };
      }
    });
  });
}

ExcelUtil.prototype.setComplexHeader = (worksheet, complexHeader, headerStyle) => {
  for (var i = 0; i < complexHeader.length; i++) {
    let header = complexHeader[i]
    for (var j = 0; j < header.length; j++) {
      let headCell = header[j]
      if (headCell.merge) {
        worksheet.mergeCells(headCell.merge.s, headCell.merge.e);
      }
      worksheet.getCell(headCell.key).value = headCell.header
    }
  }
  
  for (let index = 0; index < worksheet.columns.length; index++) {
    const element = worksheet.columns[index];
    if (index < headerStyle.length) {
      element.width = headerStyle[index].width
      if (headerStyle[index].numFmt) {
        element.numFmt = headerStyle[index].numFmt
      }
    }
  }
}

ExcelUtil.prototype.setHeaderRowStyle = (worksheet, rowIndex = 1) => {
  let head = worksheet.getRow(rowIndex);
  head.height = 22;
  head.font = {
    name: 'Microsoft YaHei UI',
    color: {
      argb: 'FF000000'
    },
    family: 2,
    size: 9,
    bold: true
  };
  head.alignment = {
    vertical: 'middle',
    horizontal: 'center'
  };
  head.eachCell((cell) => {
    cell.alignment = { vertical: 'middle', horizontal: 'center', wrapText: true };
    cell.border = {
      top: {style:'thin'},
      left: {style:'thin'},
      bottom: {style:'thin'},
      right: {style:'thin'}
    };
    cell.fill = {
      type: 'pattern',
      pattern:'gray125',
      fgColor:{argb:'FFE4E7ED'},
      bgColor:{argb:'FFE4E7ED'}
    };
  });
}

module.exports = new ExcelUtil()